//
//  ComposeViewController.swift
//  Weibo
//
//  Created by Holfioo on 15/9/10.
//  Copyright © 2015年 Holfioo. All rights reserved.
//

import UIKit

class ComposeViewController: UIViewController {

    @objc private func dissmissNav() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    @objc private func sendStatus() {
        printLog("发送")
    }
    @objc private func selectPicture() {
        printLog("增加图片")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

    }
    // 重量级方法。相当于sb中拖控件
    override func loadView() {
        view = UIView()
        view.backgroundColor = UIColor.whiteColor()
        // 设置布局
        setupNav()
        setupToolbar()
    }
    
    
    private func setupToolbar() {
        let tool = UIToolbar()
        view.addSubview(tool)
        // 解决最后一个+号的问题
        tool.backgroundColor = UIColor(white: 0.8, alpha: 1)
        // 设置约束
        let w = UIScreen.mainScreen().bounds.width
        tool.ff_AlignInner(type: ff_AlignType.BottomLeft, referView: view, size: CGSize(width: w, height: 44))
        // 按钮图片名称的字典
        let itemBtns = [["imageName" : "compose_toolbar_picture", "selector" : "selectPicture"],
                        ["imageName" : "compose_mentionbutton_background"],
                        ["imageName" : "compose_trendbutton_background"],
                        ["imageName" : "compose_emoticonbutton_background"],
                        ["imageName" : "compose_addbutton_background"]]
        
        var items = [UIBarButtonItem]()
        
        for dict in itemBtns {
//            let btn = UIButton(imageName: dict["imageName"]!)
//            // 如果字典中有此key值。
//            if let selec = dict["selector"] {
//                btn.addTarget(self, action: Selector(selec), forControlEvents: .TouchUpInside)
//            }
//            // 间隔问题
            let fixItme = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
            items.append(fixItme)
            items.append(UIBarButtonItem(imageName: dict["imageName"]!, target: self, actName: dict["selector"]))
            items.append(fixItme)
            
        }
        // 移除最后一个
        // items.removeLast()
        tool.items = items
    }
    private func setupNav() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: .Plain, target: self, action: "dissmissNav")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: .Plain, target: self, action: "sendStatus")
        // 开始的时候发送按钮并不能点击
        navigationItem.rightBarButtonItem?.enabled = false
        
        let centerV = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 32))
        let titleLab = UILabel(title: "发送微博", color: UIColor.darkGrayColor(), fontSize: 14)
        let nameLab = UILabel(title: DHUserAccountViewModel.sharedUserAccount.userAccount?.name, color: UIColor.lightGrayColor(), fontSize: 13)
        centerV.addSubview(titleLab)
        centerV.addSubview(nameLab)
        
        titleLab.ff_AlignInner(type: ff_AlignType.TopCenter, referView: centerV, size: nil)
        nameLab.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: titleLab, size: nil)
        
        navigationItem.titleView = centerV
    }
    // 懒加载控件
    
}
